package com.project.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @Author(王思翰)
 * @Description(类的描述)
 * @Date(创建时间) 2021/7/28 10:46
 */
@RestController
@RequestMapping("session")
public class SessionController {
    @RequestMapping("test")
    public String testSession(HttpServletRequest request){
        //得到session会话工厂
        HttpSession session=request.getSession();
        //在session对象中，绑定用户信息
        session.setAttribute("loginName","tom");
        return "ok";
    }

    @RequestMapping("mytest")
    public String mytest(HttpServletRequest request){
       HttpSession session= request.getSession();
       //在
       String value=(String) session.getAttribute("loginName");
        System.out.println(session.getId());
        return value;
    }

    @RequestMapping("add")
    public String add(String name,String userCode,HttpServletRequest request){
        //得到session会话对象
        HttpSession s=request.getSession();
        //从session中取出产生的验证码
        String code= (String) s.getAttribute("checkCode");
        //比较用户输入的验证码和系统验证码是否相等
        if (userCode.equalsIgnoreCase(code)== false){
            return "codeError";
        }
        System.out.println(name);
        return "ok";
    }
}
